//
//  HJNotificationSettingController.m
//  KaiChat
//
//  Created by lx on 2024/1/18.
//

#import "HJNotificationSettingController.h"
#import "HJSwitchCell.h"
#import "HJSectionHeaderView.h"
#import "HJLineCell.h"
#import "HJTitleCell.h"
#import "HJUserModel.h"
#import "HJMinePresent.h"

@interface HJNotificationSettingController ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, strong) HJUserModel *userModel;

@end

@implementation HJNotificationSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.lineView.hidden = YES;
    
    self.navigationBar.title = @"Notification Sound";
    self.tableViewStyle = UITableViewStyleGrouped;
    [self.tableView registerClass:HJSwitchCell.class forCellReuseIdentifier:@"HJSwitchCell"];
    [self.tableView registerClass:HJLineCell.class forCellReuseIdentifier:@"HJLineCell"];
    [self.tableView registerClass:HJTitleCell.class forCellReuseIdentifier:@"HJTitleCell"];
    [self.tableView registerClass:HJSectionHeaderView.class forHeaderFooterViewReuseIdentifier:@"HJSectionHeaderView"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(KNavBarHight);
        make.bottom.offset(0);
    }];
    
    [self loadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!_isOpen && section == 1) return 0;
    return [_datas[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = _datas[indexPath.section][indexPath.row];
    if ([model.identifier isEqualToString:@"lineCell"]) {
        HJLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJLineCell" forIndexPath:indexPath];
        return cell;
    }
    
    if ([model.identifier isEqualToString:@"reset"]) {
        HJTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJTitleCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    HJSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJSwitchCell" forIndexPath:indexPath];
    cell.model = model;
    TJWeakify(self)
    [cell setSwitchValueChange:^(UISwitch * _Nonnull switchView) {
        [weakself setSwitchValueChange:switchView model:model];
    }];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    if ([model.identifier isEqualToString:@"reset"]) {
        [HJMinePresent resetNoticeConfigComplete:^(NSError * _Nullable error) {
            self -> _datas = nil;
            [self loadData];
        }];
        return;
    }
    NSString *controllerName = model.controllerName;
    if (isEmptyString(controllerName)) {
        return;
    }
    [self.navigationController pushViewControllerWithName:controllerName];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (_isOpen && section == 1) return 20;
    return  CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    if ([model.identifier isEqualToString:@"lineCell"]) {
        return lineHeight;
    }
    return 50;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HJSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HJSectionHeaderView"];
    view.contentView.backgroundColor = UIColor.tj_F6F6F6Color;
    view.backgroundColor = UIColor.tj_F6F6F6Color;
    
    view.titleLabel.text = section == 1 && _isOpen ? @"Message Settings" : @"";
    view.titleLabel.textColor = UIColor.tj_BEBDCAColor;
    view.titleLabel.font = UIFontMake(12);
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}
 
- (void)setSwitchValueChange:(UISwitch * _Nonnull)switchView model:(HJCellModel *)model {
    
    HJNoticeSettingType type = [model.anyObject integerValue];
    
    [HJMinePresent setNoticeConfig:type value:!switchView.isOn complete:^(NSError * _Nullable error) {
        
        if (error) {
            return;
        }
        
        model.isOn = !switchView.isOn;
        [switchView setOn:model.isOn animated:YES];
        
        if (type == HJNoticeSettingTypeIsOpen) {
            self.isOpen = model.isOn;
            [self.tableView reloadData];
        }
    }];
}

- (void)loadData {
    dispatch_async_sql_queue(^{
        HJUserModel *userModel = [HJUserModel currentAccountModel];
        self.userModel = userModel;
        self -> _datas = self.datas;
        dispatch_async_main_queue(^{
            self.isOpen = userModel.noticeOpen;
            [self.tableView reloadData];
        });
    });
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"notifcationIsOpen";
                    model.text = @"Notifcation Sound";
                    model.textFont = UIFontMake(14);
                    model.isOn = self.userModel.noticeOpen;
                    model.anyObject = @(HJNoticeSettingTypeIsOpen);
                }],
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"preview";
                    model.text = @"Message Preview";
                    model.isOn = self.userModel.noticePreview;
                    model.textFont = UIFontMake(14);
                    model.anyObject = @(HJNoticeSettingTypePreview);
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"sound";
                    model.text = @"Alert Sound";
                    model.isOn = self.userModel.noticeSound;
                    model.textFont = UIFontMake(14);
                    model.anyObject = @(HJNoticeSettingTypeSound);
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"vibration";
                    model.text = @"Vibration";
                    model.isOn = self.userModel.noticeVibration;
                    model.textFont = UIFontMake(14);
                    model.anyObject = @(HJNoticeSettingTypeVibration);
                }],
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"lineCell";
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"reset";
                    model.text = @"Reset Notifcation Sound";
                    model.textFont = UIFontMake(14);
                    model.showArrow = NO;
                    model.showLine = NO;
                }],
            ]
        ];
    }
    return _datas;
}

@end
